﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 生成者和消费者模式Form
{
    // 定义修改输出的委托
    public delegate void ShowTextDelegate(int Index, string Addstr);

    public delegate void ChangeTextDelegate(int Index, string AddStr);
    public partial class Form2 : Form
    {
        Work work = null;
        public Form2()
        {
            InitializeComponent();
            work = new Work();
            work.ShowTextEvent += new ShowTextDelegate(changeTextValue);
        }

        private void changeTextValue(int Index, string AddStr)
        {
            TextBox ChangeTextBox = null;
            switch (Index)
            {
                case 1:
                    ChangeTextBox = this.textBox1;
                    break;
                case 2:
                    ChangeTextBox = this.textBox2;
                    break;
                case 3:
                    ChangeTextBox = this.textBox3;
                    break;
                case 4:
                    ChangeTextBox = this.textBox4;
                    break;
                default:
                    break;
            }

            if (ChangeTextBox.InvokeRequired)
            {
                ChangeTextDelegate pd = new ChangeTextDelegate(changeTextValue);
                ChangeTextBox.Invoke(pd, new object[] { Index, AddStr });
            }
            else
            {
                ChangeTextBox.AppendText(AddStr);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            work.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            work.Stop();
        }

        

    }
}
